<template>
  <div class="user-coupon">
    <div class="main-section">
      <div class="main-hd acea-row row-middle">站内信</div>
      <div class="main-bd">
        <div>
          <div class="msg-list" v-for="(item, index) in couponList" :key="index">
            <div class="msg">
              <span class="title">
                {{ item.title }}
                <div class="no-look" v-if="!item.look"></div>
              </span>
              <span class="control" @click="openRrad(index, item)">
                {{ index === selectIndex ? "收起" : "展開" }}
              </span>
            </div>
            <div class="content" v-if="index === selectIndex">
              {{ item.content }}
            </div>
          </div>
        </div>
        <div class="pages-box" v-if="total > 0">
          <el-pagination background :page-size="reqData.limit" layout="prev, pager, next" @current-change="bindPageCur"
            :total="total">
          </el-pagination>
        </div>
        <div class="empty-box" v-if="couponList.length == 0">
          <img src="~/assets/images/noCoupou.png" alt="" />
          <p class="empty-text">親，暂無消息哟~</p>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { Message } from "element-ui";
export default {
  auth: "guest",
  props: {},
  data() {
    return {
      reqData: {
        page: 1,
        limit: 7,
      },
      couponList: [],
      selectIndex: null,
      total: 0,
    };
  },
  fetch({ store }) {
    store.commit("isHeader", true);
    store.commit("isFooter", true);
  },
  head() {
    return {
      title: "站内信",
    };
  },
  computed: {},
  watch: {},
  async asyncData({ app }) { },
  created() {
    this.getMessageList();
  },
  mounted() {
    this.$nextTick(() => {
      window.scrollTo(0, 0);
      document.documentElement.scrollTop = 0;
      document.body.scrollTop = 0;
      setTimeout(() => {
        window.scrollTo(0, 0);
      }, 50);
    });
  },
  methods: {
    getMessageList() {
      this.$axios
        .get("user/message_system/list", {
          params: this.reqData,
        })
        .then((res) => {
          this.couponList = res.data.list;
          this.total = res.data.count;
        })
        .catch((err) => {
          Message.error(err);
        });
    },
    openRrad(index, data) {
      if (this.selectIndex === index) {
        this.selectIndex = null;
      } else {
        this.selectIndex = index;
        this.$axios.get(`user/message_system/detail/${data.id}`).then((res) => {
          this.couponList[index].look = true;
        });
        if (!this.couponList[index].look)
          this.$store.commit("serviceNum", this.$store.state.service_num - 1);
      }
    },
    // 分页点击
    bindPageCur(data) {
      this.reqData.page = data;
      this.selectIndex = null;
      this.getMessageList();
    },
  },
};
</script>

<style lang="scss" scoped>
.user-coupon {
  padding-top: 40px;

  .main-section {
    flex: 1;
    margin-left: 8px;
    background-color: $pure-white;
    border-radius: 8px;
    box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
  }

  .main-hd {
    height: 60px;
    border-bottom: 1px solid $light-gray;
    font-size: 18px;
    color: $ivory;
    padding: 0 20px;
    font-weight: 600;
  }

  .main-bd {
    padding: 20px;
  }

  .msg-list {
    margin-bottom: 16px;
    border-radius: 8px;
    overflow: hidden;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);

    .msg {
      display: flex;
      padding: 16px 20px;
      justify-content: space-between;
      align-items: center;

      .title {
        color: $ivory;
        display: flex;
        align-items: center;
        font-weight: 500;

        .no-look {
          width: 8px;
          height: 8px;
          margin-left: 6px;
          background-color: $gold-accent;
          border-radius: 50%;
        }
      }

      .control {
        color: $ivory;
        cursor: pointer;
        padding: 4px 8px;
        border-radius: 4px;
        transition: all 0.3s ease;

        &:hover {
          background-color: $gold-accent;
        }
      }
    }

    .content {
      padding: 20px;
      color: $ivory;
      line-height: 1.6;
      border-top: 1px solid $light-gray;
    }
  }

  .empty-box {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 60px 0;

    img {
      width: 120px;
      height: 120px;
      opacity: 0.8;
    }

    .empty-text {
      margin-top: 24px;
      color: $ivory;
      font-size: 16px;
    }
  }

  .pages-box {
    display: flex;
    justify-content: center;
    margin-top: 24px;
    padding-bottom: 16px;

    ::v-deep .el-pagination {
      .el-pager li.active {
        background-color: $obsidian;
        border-color: $obsidian;
        color: $pure-white;
      }

      .el-pagination {
        color: $obsidian;

        button {
          color: $obsidian;
        }
      }

      .btn-prev,
      .btn-next,
      .btn-prev:hover,
      .btn-next:hover,
      .el-icon-arrow-left,
      .el-icon-arrow-right,
      .el-icon-arrow-left:hover,
      .el-icon-arrow-right:hover {
        color: $obsidian;
      }

      .el-pager li:hover {
        color: $obsidian;
      }

      .el-pager li.number.active {
        background-color: $obsidian;
        border-color: $obsidian;
        color: $pure-white;
      }
    }
  }
}
</style>
